package com.pipayshop.controller;

import com.pipayshop.domain.ShopCategory;
import com.pipayshop.domain.vo.ShopCartVo;
import com.pipayshop.domain.vo.ShopCategoryVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/9 15:13
 * @description 网店分类接口
 */
@Tag(name  = "网店商品分类接口",description  = "网店商品分类接口")
@RestController
@RequestMapping("/pipayshopapi/category")
@Slf4j
public class OnlineStoreShopCategoryController {
    @Autowired
    private OnlineStoreShopCategoryService onlineStoreShopCategoryService;

    /**
     * 获取网店商品分类
     * @param categoryPid 分类父id
     * @return 分类对象集合
     */
    @GetMapping("/selectAllCategory/{categoryPid}")
    @Operation(summary ="获取网店商品分类")
    public ResponseVO<List<ShopCategory>> list(@PathVariable Long categoryPid) {
        try {
            return ResponseVO.getSuccessResponseVo(onlineStoreShopCategoryService.getList(categoryPid));
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_product_category_failed");
        }

    }

    @GetMapping("selectAll")
    @Operation(summary = "查询全部数据")
    public ResponseVO<List<ShopCategory>> selectAll(){
        try {
            return ResponseVO.getFalseResponseVo(onlineStoreShopCategoryService.selectAll());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("Data query failure");
        }
    }
    @GetMapping("/selectList")
    @Operation(summary = "管理端查询全部数据")
    public ResponseVO<List<ShopCategoryVO>> selectList(){
        try {
            List<ShopCategoryVO> list = onlineStoreShopCategoryService.selectList();
            return ResponseVO.getSuccessResponseVo(list);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("Data query success");
        }
    }

    @PostMapping("/save")
    @Operation(summary = "添加或修改")
    public ResponseVO save(@RequestBody ShopCategory category){
        try {
            String andupdate = onlineStoreShopCategoryService.insertAndupdate(category);
            return ResponseVO.getSuccessResponseVo(andupdate);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("Failed to add or modify");
        }
    }



}
